﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// KnowledgeOperation 的摘要说明
/// </summary>
public class KnowledgeOperation
{
    private Dbcom cm;

    public KnowledgeOperation(Dbcom CM)
    {
        cm = CM;
    }

    public DataSet GetDataSetTABLE_F()
    {       
        DataSet ds = Dbcom.GetDataSet("SELECT * FROM TABLE_F ORDER BY PARENTID");
        return ds;

    }

    /// <summary>
    /// 获取table_f数据集合
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public DataSet GetDataSetTABLE_E(string id)
    {       
        string sql = string.Format("SELECT * FROM TABLE_E WHERE SIMPLEMETAID_F = '{0}' ORDER BY NUM", id);
        DataSet ds = Dbcom.GetDataSet(sql);
        return ds;
    }

    /// <summary>
    /// 删除Table_F指定记录
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="parentId"></param>
    /// <returns></returns>
    public int DeleteTable_F(string ID, string parentId)
    {       
        string sql = string.Format("DELETE TABLE_F WHERE ID = '{0}' or parentId = '{1}'"
                                       , ID, parentId);
        return Dbcom.ExecuteSQL(sql);
    }

    /// <summary>
    /// 获取et_document中的Object
    /// </summary>
    /// <param name="ObjectId"></param>
    /// <returns></returns>
    public DataSet GetObjectDataById(string ObjectId)
    {      
        string sql = string.Format("select OBJECTDATA from et_document where objectid={0}"
                                       , ObjectId);
        //"select OBJECTDATA from et_document where objectid=" + ObjectId + "";
        return Dbcom.GetDataSet(sql);
    }

    /// <summary>
    /// 保存简单元素
    /// </summary>
    /// <returns></returns>
    public int SaveSimpleElement(string SaveType,string txtNum,string txtName,string txtCode,string selnodeText,string selnodeTag,ref string id)
    {       
        string sql = "";
        if (SaveType == "ADD")
        {
            id = Dbcom.GenId().ToString();
            //添加
            sql = string.Format("INSERT INTO TABLE_E(NUM,EVALUE,NAME,SIMPLEMETAID_F,id)VALUES('{0}','{1}','{2}','{3}','{4}')"
                                       , txtNum
                                       , txtName
                                       , selnodeText
                                       , selnodeTag, id);
        }
        else
        {
           sql= string.Format("UPDATE TABLE_E SET NUM = '{0}' , EVALUE = '{1}' , EVALUECODE = '{2}' WHERE ID = '{3}'"
                                , txtNum
                                , txtName
                                , txtCode
                                , id);
        }
        return Dbcom.ExecuteSQL(sql);
    }

    /// <summary>
    /// 保存复杂元素
    /// </summary>
    /// <returns></returns>
    public int SaveComplexElement(string SaveType, string tvDirectorySelectedText, string tvListSelectedNodeName, 
        string txtComplexName, string tvDirectorySelectedTag, string tvListSelectedNodeTag, string tvListCount,
        string DocXML,ref string id,ref string evalueCode)
    {      
        string sql;
        string sql2;
        DBParameter[] op;
        if (SaveType == "ADD")
        {
            //添加
            DataTable dt = Dbcom.GetDataSet("select max(objectid) from ET_DOCUMENT").Tables[0];             
            if (dt.Rows.Count > 0)
            {
                evalueCode = (Convert.ToInt32(dt.Rows[0][0].ToString()) + 1).ToString();
            }
            id = Dbcom.GenId().ToString(); 
            sql = string.Format("insert into TABLE_E(name,EVALUE,EVALUECODE,SIMPLEMETAID_F,ITEM_STYLE,NUM,ID)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')"
                                       , tvDirectorySelectedText
                                       , txtComplexName
                                       , evalueCode
                                       , tvDirectorySelectedTag
                                       , "100"
                                       , tvListCount, id);

          
            sql2 = string.Format("insert into ET_DOCUMENT(OBJECTID,OBJECTNAME,OBJECTTYPE,OBJECTDATA)values('{0}','{1}','{2}',:doc)"
                                        , evalueCode
                                        , txtComplexName
                                        , "-1");
            op = new DBParameter[] { new DBParameter() };
            op[0].ParameterName = "doc";
            op[0].DBType = OracleDbType.Clob;
            op[0].Value = DocXML;

        }
        else
        {
            //修改操作
            sql = string.Format("UPDATE TABLE_E SET EVALUE = '{0}'  WHERE ID = '{1}'"
                                                     , txtComplexName
                                                     , tvListSelectedNodeName);          
            sql2 = string.Format("UPDATE ET_DOCUMENT SET OBJECTDATA=:doc,OBJECTNAME='{0}' where objectid='{1}'"
                                        , txtComplexName
                                        , tvListSelectedNodeTag);
            op = new DBParameter[] { new DBParameter() };
            op[0].ParameterName = "doc";
            op[0].DBType = OracleDbType.Clob;
            op[0].Value = DocXML;
        }
        if (Dbcom.ExecuteSQL(sql) > 0 && Dbcom.ExecuteSQLWithParams(sql2, op) > 0)
        {
            return 2;
        }
        else
        {
            return 0;
        }

    }

    /// <summary>
    /// 删除元素
    /// </summary>
    /// <returns></returns>
    public int ElementDelete(string delType,string id,string objectid)
    {
        try
        {           
            string sql = string.Format("DELETE TABLE_E WHERE ID = '{0}'", id);
            string sql2 = "";
            if (delType == "ET_DOCUMENT")
            {
                sql2 = string.Format("DELETE ET_DOCUMENT WHERE objectid ='{0}'", id);
            }
            int cot = 0;
            cot = Dbcom.ExecuteSQL(sql);
            if (cot > 0)
            {
                if (sql2 != "")
                    cot=Dbcom.ExecuteSQL(sql2);
            }

            return cot;
        }
        catch
        {
            return 0;
        }
                     
    }
}